package com.frank.subject.domain.service.impl;

import com.frank.subject.common.enums.CategoryTypeEnum;
import com.frank.common.enums.IsDeletedFlagEnum;
import com.frank.subject.domain.convert.SubjectLabelBoConverter;
import com.frank.subject.domain.entity.SubjectLabelBo;
import com.frank.subject.domain.service.SubjectLabelDomainService;
import com.frank.subject.infra.basic.entity.SubjectLabel;
import com.frank.subject.infra.basic.entity.SubjectMapping;
import com.frank.subject.infra.basic.service.SubjectLabelService;
import com.frank.subject.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author Climb Zou
 * @version v0.0.1
 * @date 2024/1/23 14:27
 */
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    private final SubjectLabelService subjectLabelService;

    private final SubjectMappingService subjectMappingService;


    public SubjectLabelDomainServiceImpl(SubjectLabelService subjectLabelService,
                                         SubjectMappingService subjectMappingService) {
        this.subjectLabelService = subjectLabelService;
        this.subjectMappingService = subjectMappingService;
    }

    @Override
    public void insert(SubjectLabelBo subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectLabelService.save(subjectLabel);
    }

    @Override
    public Boolean update(SubjectLabelBo subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        return subjectLabelService.updateById(subjectLabel) ;
    }

    @Override
    public Boolean deleteByPrimary(SubjectLabelBo subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        return subjectLabelService.updateById(subjectLabel) ;
    }

    @Override
    public List<SubjectLabelBo> queryLabelByCategoryId(SubjectLabelBo subjectLabelBO) {
        // 如果查询的是岗位大类，将所有标签查询出来
        if (CategoryTypeEnum.PRIMARY.getCode() == subjectLabelBO.getCategoryId()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> subjectLabels = subjectLabelService.queryListByCondition(subjectLabel);
            return SubjectLabelBoConverter.INSTANCE.convertLabelListToBOList(subjectLabels);
        }

        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(subjectMapping.getCategoryId());
        List<Long> labelIds = subjectMappingService.queryLabelId(subjectMapping);
        if (labelIds.isEmpty()){
            return Collections.emptyList();
        }
        List<SubjectLabel> subjectLabels = subjectLabelService.queryBatchLabelByIds(labelIds);
        subjectLabels.forEach(subjectLabel -> subjectLabel.setCategoryId(subjectLabelBO.getCategoryId()));
        return SubjectLabelBoConverter.INSTANCE.convertLabelListToBOList(subjectLabels);
    }
}
